package com.fc.app.fcapp

import android.content.Context
import android.net.ConnectivityManager
import android.os.Handler
import android.os.HandlerThread
import android.os.Looper
import java.util.concurrent.Executors
import java.util.concurrent.ThreadFactory

/**
 * Created by fc on 2017/10/31.
 */

private val uiHandler: Handler by lazy {
    Handler(Looper.getMainLooper())
}

private val delayHandler: Handler by lazy {
    val ht = HandlerThread("DelayHandlerThread")
    ht.start()
    Handler(ht.looper)
}

private val ioThreadPool by lazy {
    Executors.newCachedThreadPool(object : ThreadFactory {
        var count = 0
        override fun newThread(r: Runnable?): Thread {
            val t = Thread(r, "io_pool_thread_${count++}")
            t.setUncaughtExceptionHandler { thread, ex ->
                ex.printStackTrace()
            }
            return t
        }
    })
}
private val workerThreadPool by lazy {
    Executors.newCachedThreadPool(
            object : ThreadFactory {
                var count = 0
                override fun newThread(r: Runnable?): Thread {
                    val t = Thread(r, "worker_pool_thread_${count++}")
                    t.setUncaughtExceptionHandler { thread, ex ->
                        ex.printStackTrace()
                    }
                    return t
                }
            })
}

private val singleThread by lazy {
    Executors.newSingleThreadExecutor()
}

fun ioThread(r: Runnable) {
    ioThreadPool.execute(r)
}

fun ioThread(r: () -> Unit) {
    ioThreadPool.execute(r)
}

fun ioThread(delay: Long, r: () -> Unit) {
    delayHandler.postDelayed({
        ioThreadPool.execute(r)
    }, delay)
}

fun singleThread(r: () -> Unit) {
    singleThread.execute(r)
}

fun workerThread(r: () -> Unit) {
    workerThreadPool.execute(r)
}

fun workerThread(delay: Long, r: () -> Unit) {
    delayHandler.postDelayed({
        workerThreadPool.execute(r)
    }, delay)
}

fun uiThread(r: () -> Unit) {
    if (Looper.myLooper() == Looper.getMainLooper())
        r()
    else
        uiHandler.post(r)
}

fun uiThread(delay: Long, r: () -> Unit) {
    uiHandler.postDelayed(r, delay)
}

fun runOnUiThread(block: () -> Unit) {
    if (Looper.myLooper() == Looper.getMainLooper())
        block()
    else
        uiHandler.post { block() }
}

fun runOnUiThread(delay: Long, block: () -> Unit) {
    uiHandler.postDelayed(block, delay)
}

fun runAsync(block: () -> Unit) {
    ioThread { block() }
}

fun runAsync(delay: Long, block: () -> Unit) {
    ioThread {
        Thread.sleep(delay)
        block()
    }
}

fun isNetConnected(mContext: Context): Boolean {
    try {
        val connectivityManager = mContext.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val info = connectivityManager.activeNetworkInfo
        val isConnected = info != null && info.isAvailable && info.isConnected
        return isConnected
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return false
}
